package com.doge.service.admin.lsnotice;

import com.doge.bean.bo.NoticeBo;
import com.doge.bean.pojo.notice.NoticeAdmin;
import com.doge.bean.pojo.notice.NoticeAdminExample;
import com.doge.bean.vo.notice.NoticeListVo;
import com.doge.mapper.NoticeAdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: doge
 * @description: 通知服务
 * @author: Mr.Jiang
 * @create: 2021-12-04 14:00
 **/
@Service
public class LsnoticeServiceImpl implements LsnoticeService{
    @Autowired
    NoticeAdminMapper noticeAdminMapper;

    @Override
    public NoticeListVo list(Integer page, Integer limit, String title, String type, String sort, String order) {
        PageHelper.startPage(page,limit);
        NoticeAdminExample noticeAdminExample = new NoticeAdminExample();
        NoticeAdminExample.Criteria criteria = noticeAdminExample.createCriteria().andDeletedEqualTo(false);
        noticeAdminExample.setOrderByClause(sort+" "+order);
        if("unread".equals(type)){
            criteria.andReadTimeIsNull();
        }
        else if ("read".equals(type)) {
            criteria.andReadTimeIsNotNull();
        }
        if(title!=""){
            criteria.andNoticeTitleLike("%"+title+"%");
        }
        List<NoticeAdmin> noticeAdmins = noticeAdminMapper.selectByExample(noticeAdminExample);
        PageInfo pageInfo = new PageInfo(noticeAdmins);
        NoticeListVo noticeListVo = new NoticeListVo();
        noticeListVo.setLimit(limit);
        noticeListVo.setList(noticeAdmins);
        noticeListVo.setPage(page);
        noticeListVo.setTotal(Math.toIntExact(pageInfo.getTotal()));
        noticeListVo.setPages(pageInfo.getPages());
        return noticeListVo;
    }

    @Override
    public void update(NoticeAdmin noticeAdmin) {
        if(noticeAdmin.getDeleted()==false){
            noticeAdmin.setReadTime(new Date());
        }
        noticeAdmin.setUpdateTime(new Date());
        noticeAdminMapper.updateByPrimaryKeySelective(noticeAdmin);
    }

    @Override
    public void batchReadNotice(NoticeBo noticeBo) {
        NoticeAdmin noticeAdmin = new NoticeAdmin();
        noticeAdmin.setReadTime(new Date());
        for (Object id : noticeBo.getIds()) {
            NoticeAdminExample noticeAdminExample = new NoticeAdminExample();
            noticeAdminExample.createCriteria().andIdEqualTo((Integer) id).andDeletedEqualTo(false);
            noticeAdminMapper.updateByExampleSelective(noticeAdmin,noticeAdminExample);
        }
    }

    @Override
    public void brmnotice(NoticeBo noticeBo) {
        NoticeAdmin noticeAdmin = new NoticeAdmin();
        noticeAdmin.setDeleted(true);
        for (Object id : noticeBo.getIds()) {
            NoticeAdminExample noticeAdminExample = new NoticeAdminExample();
            noticeAdminExample.createCriteria().andIdEqualTo((Integer) id).andDeletedEqualTo(false);
            noticeAdminMapper.updateByExampleSelective(noticeAdmin,noticeAdminExample);
        }
    }
}
